
function deleteProduct(e) {
    var deleteData;
    if (e == undefined) {
        var arr = [];
        $(".product-check").each(function () {
            var getTarget = $(this);
            if (getTarget.prop('checked')) {
                arr.push(getTarget.val());
            }
        });
        if (arr.length == 0) {
            alert("请选择要删除的数据");
            return;
        }
        deleteData = '[' + arr.toString() + ']';
    } else {
        deleteData = '[' + $(e).attr('id').substring(22) + ']';
    }
    $.ajax({
        url: '/product/admin/delete',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        data: deleteData,
        success: function (res) {
            if (res.code != 200) {
                alert(res.message);
                return;
            }
            alert("删除成功");
            show();
        }

    })
}

// id获取商品
function idGetProduct(id) {
    var getData;
    $.ajax({
        url: '/product/admin/' + id,
        type: 'get',
        dataType: 'json',
        async: false,
        success: function (res) {
            if (res.code != 200) {
                alert('查询失败!');
                return;
            }
            getData = res;
        }

    })
    return getData;
}

// 分页查询商品
function pageProduct(pageNum, pageSize, name, categoryName) {
    var getData;
    $.ajax({
        url: '/product/list',
        type: 'get',
        dataType: 'json',
        async: false,
        data: {
            name: name,
            categoryName: categoryName,
            pageNum: pageNum,
            pageSize: pageSize
        },
        success: function (res) {
            if (res.code != 200) {
                alert('查询失败!');
                return;
            }
            getData = res;
        }

    })
    return getData;
}

// 保存修改的商品
function updateProduct() {
    let img = $('#product_img').attr("src");
    if (img == null || img == undefined) {
        img = '';
    }
    let product = {
        "id": $('#product_id').val(),
        "code": $('#product_code').val(),
        "title": $('#product_title').val(),
        "categoryId": $('#product_categoryId').val(),
        "img": img,
        "price": $('#product_price').val(),
        "stocks": $('#product_stocks').val(),
        "description": $('#product_description').val()
    }
    $.ajax({
        url: '/product/admin/update',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(product),
        success: function (res) {
            if (res.code != 200) {
                alert(res.message);
                return;
            }
            $("#product_modal_close").click();
            show();
            alert("保存成功");
        }
    })
}

// 添加商品
function addProduct() {
    let img = $('#product_img').attr("src");
    if (img == null || img == undefined) {
        img = '';
    }
    let product = {
        "code": $('#product_code').val(),
        "title": $('#product_title').val(),
        "categoryId": $('#product_categoryId').val(),
        "img": img,
        "price": $('#product_price').val(),
        "stocks": $('#product_stocks').val(),
        "description": $('#product_description').val()
    }
    $.ajax({
        url: '/product/admin/insert',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(product),
        success: function (res) {
            if (res.code != 200) {
                alert(res.message);
                return;
            }
            $("#product_modal_close").click();
            show();
            alert("保存成功");
        }
    })
}
